package com.test08.excelpoi04;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gson.Gson;
import com.test08.excelpoi02.NFCBean;
import com.test08.excelpoi02.SummaryBean;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Boolean;
import jxl.write.DateFormats;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/*
 * 用来处理1.xls中的站点中信息的类
 * 
 */
public class ExcelTest04<V> {
	public static Map<Double, Double> map = new HashMap<>();

	public static void init() throws Exception{
		jxl.Workbook readwb = null;
		InputStream instream = new FileInputStream("src/6.xls");
		readwb = Workbook.getWorkbook(instream);

		Sheet readsheet = readwb.getSheet(0);
		int rsColumns = readsheet.getColumns();
		int rsRows = readsheet.getRows();
		// 假设真实数据从第4行开始的
		String content = null;
		Double col0 = null;
		Double col2 = null;

		for (int i = 2; i < rsRows; i++) {
			for (int j = 0; j < rsColumns; j++) {
				try {
					switch (j) {
					case 0:
						content = readsheet.getCell(j, i).getContents().trim();
						if (!"".equals(content)) {
							col0 = Double.parseDouble(content);
						}
						break;
					case 2:
						content = readsheet.getCell(j, i).getContents().trim();
						if (!"".equals(content)) {
							col2 = Double.parseDouble(content);
						}
						break;
					default:
						break;
					}
					
					if (col0 != null && col2 != null) {
						map.put(col0, col2);
					}
				} catch (Exception e) {
				}
			}
		}
	}
	public static Double convert(Double src) throws Exception{
		if (map.isEmpty()) {
			init();
		}
		Set<Double> keySet = map.keySet();
//		先假设第一个key就是最小的
		Double minKey = keySet.iterator().next();
		double minDiffer = Math.abs(src-map.get(minKey));
//		遍历，查看有无更小的 
		double differ;
		for (Double key : keySet) {
			differ = Math.abs(src-map.get(key));
			if(differ < minDiffer){
				minKey = key;
				minDiffer = differ;
			}
		}
		return minKey;
	}

}
